package com.hdl.servlet.cart;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdl.entity.HdlUser;
import com.hdl.entity.MallCart;

/**
 * ajax查看购物车
 * @author Administrator
 *
 */
@WebServlet("/myCart.let")
public class SeeMyCartServlet extends HttpServlet{
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//查看用户是否登陆
		HdlUser user = (HdlUser) req.getSession().getAttribute("user");
		Map<String,Object> map = new HashMap<String,Object>();
		ObjectMapper mapper = new ObjectMapper();
		//设置相应头
		resp.setContentType("application/json;charset=utf-8");
		if(user == null) {
			//没有登录
			map.put("code", 1);
			//写回消息
			mapper.writeValue(resp.getOutputStream(), map);
		}
		
		//查看是否添加商品进入购物车
		MallCart cart = (MallCart) req.getSession().getAttribute("cart");
		if(cart == null) {
			//没有添加
			map.put("code", 2);
			//写回消息
			mapper.writeValue(resp.getOutputStream(), map);
		}
		
		//既登陆了又添加了商品
		map.put("code", 3);
		//写回消息
		mapper.writeValue(resp.getOutputStream(), map);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
